package org.ala.distributed_transaction_commons.communication;

import java.util.HashMap;
import java.util.Map;

import org.ala.distributed_transaction_commons.communication.Message.MessageContent;
import org.ala.distributed_transaction_commons.communication.coordinator.CoordinatorPublisher;
import org.ala.distributed_transaction_commons.communication.participant.ParticipantPublisher;
import org.ala.distributed_transaction_commons.redis.DistributedTransactionRedisTools;
import org.springframework.util.Assert;

/**
 * action执行器统一逻辑
 *
 * @author 骆毅(ala)
 * @date 2024年2月19日
 */
public abstract class AbstractActionExector {

	
	/**
	 * redis执行器
	 */
	protected DistributedTransactionRedisTools distributedTransactionRedisTools;
	/**
	 * 向协调者publisher
	 */
	protected CoordinatorPublisher coordinatorPublisher;
	/**
	 * 向参与者publisher
	 */
	protected ParticipantPublisher participantPublisher;
	
	
	/**
	 * action holder
	 */
	protected Map<String, IAction> actionHolder = new HashMap<>();
	/**
	 * 追加action
	 */
	protected void appendAction(AbstractAction action) {
		action.setDistributedTransactionRedisTools(distributedTransactionRedisTools);
		action.setCoordinatorPublisher(coordinatorPublisher);
		action.setParticipantPublisher(participantPublisher);
		actionHolder.put(action.type(), action);
	}
	
	
	/**
	 * 执行msg
	 */
	public void doAction(MessageContent mc) {
		//	取action
		IAction action = actionHolder.get(mc.getMsgType());
		Assert.isTrue(action != null, "[" + this.getClass().getSimpleName() + " doAction] 根据type取不到action type:" + mc.getMsgType());
	
		action.execute(mc);
	}


	public DistributedTransactionRedisTools getDistributedTransactionRedisTools() {
		return distributedTransactionRedisTools;
	}
	public void setDistributedTransactionRedisTools(DistributedTransactionRedisTools distributedTransactionRedisTools) {
		this.distributedTransactionRedisTools = distributedTransactionRedisTools;
	}
	public CoordinatorPublisher getCoordinatorPublisher() {
		return coordinatorPublisher;
	}
	public void setCoordinatorPublisher(CoordinatorPublisher coordinatorPublisher) {
		this.coordinatorPublisher = coordinatorPublisher;
	}
	public ParticipantPublisher getParticipantPublisher() {
		return participantPublisher;
	}
	public void setParticipantPublisher(ParticipantPublisher participantPublisher) {
		this.participantPublisher = participantPublisher;
	}
	
}
